//
//  XMPMainTabbarVC.m
//  MonkeyPlayVideo
//
//  Created by jingen zhuang on 2024/6/19.
//

#import "XMPMainTabbarVC.h"
#import "XMPBaseNavVC.h"
#import "XMPHomeViewController.h"
#import "XMPRecommondVC.h"

@implementation ACSTabbarItemModel

@end


@interface XMPMainTabbarVC ()<ACSTabbarDelegate>

@property (nonatomic, weak) UIViewController *selectVC;
@property(nonatomic, assign) NSInteger selectIndex;

@end

@implementation XMPMainTabbarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addChildCustomViewController];
    // Do any additional setup after loading the view.
}

- (void)addChildCustomViewController
{
    ACSTabbarItemModel *model1 = [ACSTabbarItemModel new];
    model1.targetVC = [XMPBaseNavVC rootVC:[XMPHomeViewController new]];
    model1.normalImg = @"ic_home_nxz";
    model1.selectImg = @"ic_home_xz";
    
    ACSTabbarItemModel *model2 = [ACSTabbarItemModel new];
    model2.targetVC = [XMPBaseNavVC rootVC:[XMPRecommondVC new]];
    model2.normalImg = @"ic_tj_wxz";
    model2.selectImg = @"ic_tj_xz";
    
    ACSTabbarItemModel *model3 = [ACSTabbarItemModel new];
    model3.targetVC = [XMPBaseNavVC rootVC:[XMPHomeViewController new]];
    model3.normalImg = @"ic_bd_wxz";
    model3.selectImg = @"ic_bd_xz";
    
    ACSTabbarItemModel *model4 = [ACSTabbarItemModel new];
    model4.targetVC = [XMPBaseNavVC rootVC:[XMPHomeViewController new]];
    model4.normalImg = @"ic_my_nmr";
    model4.selectImg = @"ic_my_mr";
    
    [self.itemArr addObject:model1];
    [self.itemArr addObject:model2];
    [self.itemArr addObject:model3];
    [self.itemArr addObject:model4];
    
    
    NSMutableArray *tabBarConfs = @[].mutableCopy;
    NSMutableArray *tabBarVCs = @[].mutableCopy;
    [self.itemArr enumerateObjectsUsingBlock:^(ACSTabbarItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NRFBaseTabBarConfigModel *model = [NRFBaseTabBarConfigModel new];
        model.itemTitle = obj.title;
        model.selectImageName = obj.selectImg;
        model.normalImageName = obj.normalImg;
        model.itemLayoutStyle = HKBaseTabBarItemLayoutStylePicture;
        model.componentMargin = UIEdgeInsetsMake(0, 0, 0, 0);
        model.interactionEffectStyle = HKBaseTabBarInteractionEffectStyleSpring;
        model.tag = 100 + idx;
        model.itemBadgeStyle = HKBaseTabBarItemBadgeStyleTopRight;
        model.automaticHidden = YES;
        model.icomImgViewSize = CGSizeMake(MPUAdapted(56),MPUAdapted(49));
        model.selectBackgroundColor = UIColor.clearColor;
        model.normalBackgroundColor = UIColor.clearColor;
        UIViewController *vc = obj.targetVC;
        [tabBarVCs addObject:vc];
        [tabBarConfs addObject:model];
    }];
    
    self.viewControllers = tabBarVCs;
    self.hkTabBar = [NRFTabbar new];
//    [self updateTabbarConfig:tabBarConfs idx:0];
    self.hkTabBar.tabBarConfig = tabBarConfs;
    self.hkTabBar.backgroundColor = ColorWhite;
    self.hkTabBar.delegate = self;
    [self.tabBar addSubview:self.hkTabBar];
    [self addLayoutTabBar];
    
    self.selectIndex = self.hkTabBar.selectIndex;
    
    if (@available(iOS 13, *))
    {
        UITabBarAppearance *appearance = [self.tabBar.standardAppearance copy];
        appearance.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
        appearance.backgroundImage = [UIImage imageWithColor:[UIColor clearColor]];
        [appearance configureWithTransparentBackground];
        self.tabBar.standardAppearance = appearance;
    }
    else
    {
        self.tabBar.backgroundImage = [UIImage new];
        self.tabBar.shadowImage = [UIImage new];
    }
}

#pragma mark - tabbar delegate
- (void)hkBaseTabBar:(NRFTabbar *)tabbar selectIndex:(NSInteger)index
{
    UIViewController *vc = self.viewControllers[index];
    self.selectVC = vc;
    self.selectIndex = index;
    [self setSelectedIndex:index];
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex{
    [super setSelectedIndex:selectedIndex];
    if(self.hkTabBar)
    {
        self.hkTabBar.selectIndex = selectedIndex;
    }
}

- (void)addLayoutTabBar{
    // 使用重载viewDidLayoutSubviews实时计算坐标 （下边的 -viewDidLayoutSubviews 函数）
    // 能兼容转屏时的自动布局
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.hkTabBar.frame = self.tabBar.bounds;
    [self.hkTabBar viewDidLayoutItems];
}

#pragma mark - lazy load

- (NSMutableArray<ACSTabbarItemModel *> *)itemArr {
    if (!_itemArr) {
        _itemArr = [[NSMutableArray alloc] init];
    }
    return _itemArr;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

@implementation UIViewController(__ACSTabType)
-(CATabbarItemType)tabbarType{
    return [objc_getAssociatedObject(self, @selector(tabbarType)) integerValue];
}
-(void)setTabbarType:(CATabbarItemType)tabbarType{
    objc_setAssociatedObject(self, @selector(tabbarType), @(tabbarType), OBJC_ASSOCIATION_RETAIN);
}

@end
